package com.zhentao.equipment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.equipment.mapper.DeviceMaintenancePlanMapper;
import com.zhentao.equipment.mapper.DeviceMaintenanceRecordMapper;
import com.zhentao.pojo.DeviceMaintenancePlan;
import com.zhentao.pojo.DeviceMaintenanceRecord;
import com.zhentao.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 维护计划与记录服务：计划CRUD、状态流转、记录上报
 */
@Service
public class DeviceMaintenanceService {

    @Autowired
    private DeviceMaintenancePlanMapper planMapper;

    @Autowired
    private DeviceMaintenanceRecordMapper recordMapper;

    /** 新增或更新维护计划（status：1-待执行，2-执行中，3-已完成） */
    public ResultVo savePlan(DeviceMaintenancePlan plan) {
        if (plan.getPlanId() == null) {
            plan.setStatus(1);
            planMapper.insert(plan);
        } else {
            planMapper.updateById(plan);
        }
        return ResultVo.success();
    }

    /** 删除计划 */
    public ResultVo deletePlan(Long planId) {
        planMapper.deleteById(planId);
        return ResultVo.success();
    }

    /** 计划分页 */
    public ResultVo planPage(Integer pageNum, Integer pageSize, Long deviceId, Integer status) {
        Page<DeviceMaintenancePlan> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<DeviceMaintenancePlan> qw = new LambdaQueryWrapper<>();
        if (deviceId != null) qw.eq(DeviceMaintenancePlan::getDeviceId, deviceId);
        if (status != null) qw.eq(DeviceMaintenancePlan::getStatus, status);
        qw.orderByDesc(DeviceMaintenancePlan::getPlanDate);
        return ResultVo.success(planMapper.selectPage(page, qw));
    }

    /** 计划状态流转：待执行->执行中/已完成/取消(自定义) */
    public ResultVo changePlanStatus(Long planId, Integer status) {
        DeviceMaintenancePlan plan = planMapper.selectById(planId);
        if (plan == null) return ResultVo.error(400, "计划不存在");
        plan.setStatus(status);
        planMapper.updateById(plan);
        return ResultVo.success();
    }

    /** 新增维护记录，并联动计划状态置为已完成 */
    public ResultVo addRecord(DeviceMaintenanceRecord record) {
        record.setCreateTime(new Date());
        recordMapper.insert(record);
        if (record.getPlanId() != null) {
            DeviceMaintenancePlan plan = planMapper.selectById(record.getPlanId());
            if (plan != null) {
                plan.setStatus(3);
                planMapper.updateById(plan);
            }
        }
        return ResultVo.success();
    }

    /** 维护记录分页 */
    public ResultVo recordPage(Integer pageNum, Integer pageSize, Long deviceId, Long planId) {
        Page<DeviceMaintenanceRecord> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<DeviceMaintenanceRecord> qw = new LambdaQueryWrapper<>();
        if (deviceId != null) qw.eq(DeviceMaintenanceRecord::getDeviceId, deviceId);
        if (planId != null) qw.eq(DeviceMaintenanceRecord::getPlanId, planId);
        qw.orderByDesc(DeviceMaintenanceRecord::getMaintenanceDate);
        return ResultVo.success(recordMapper.selectPage(page, qw));
    }
}





